﻿using Blm.Utils.Extensions;
using System;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Schedule;
using VisionCloud.Domain.Common;
using VisionCloud.Domain.Configs;
using VisionCloud.Domain.DtoModel.Rs;
using VisionCloud.Domain.Extension;
using VisionCloud.Domain.Repository.Interface.Rs;

namespace VisionCloud.Domain.Repository.Implement.Rs
{
    public class RsWorkOrderRepository : BaseRepository<RsWorkOrder>, IRsWorkOrderRepository
    {
        public async Task<ApiPageResult> GetPage(WorkOrderListRequestDto condition)
        {
            if (string.IsNullOrWhiteSpace(condition.OrderBy))
            {
                condition.OrderBy = "CreatedDate";
                condition.AscDesc = "DESC";
            }
            // 公司Id
            var companyGuid = OperatorProvider.Get().CompanyGuid;
            // 注意内部公司和外部公司的权限
            var sysOrg = Db.SysOrg.FirstOrDefault(p => p.Id == companyGuid && !p.IsDeleted);
            var sourceType = (sysOrg.IsInterOrg ?? false) ? 1 : 2;
            var ids = Db.RsWorkOrderOrg.Where(x => x.SysOrgId == companyGuid && !x.IsDeleted).Select(x => x.WorkOrderId.ToString());

            var query = from p in Db.RsWorkOrder
                        join t in Db.RsWorkOrderMap.Where(x => x.CompanyGuid == companyGuid && !x.IsDeleted) on p.Id equals t.RsWorkOrderId into t1
                        from tt1 in t1.DefaultIfEmpty()
                        join m in Db.MsProjects on tt1.ProjectId equals m.ProjectId into t2
                        from tt2 in t2.DefaultIfEmpty()
                        where !p.IsDeleted
                        && (string.IsNullOrWhiteSpace(condition.Query) || p.BpsTaskId.Contains(condition.Query) || p.ProjectName.Contains(condition.Query) || p.ProjectId.Contains(condition.Query))
                        select new WorkOrderListResponseModel
                        {
                            Id = p.Id.ToString(),
                            ProjectId = tt2.ProjectId.ToString(),
                            ProjectName = p.ProjectName,
                            ProjectNo = tt2.ProjectNo,
                            BpsTaskId = p.BpsTaskId,
                            ProcedureCode = p.ProcedureCode,
                            ProcedureName = WorkOrderCodeAndNameMap.GetNameWithCode(p.ProcedureCode),
                            BpsTaskRemark = p.BpsTaskRemark,
                            Status = p.Status,
                            StatusStr = p.Status.ToString(),
                            BpsTaskType = p.BpsTaskType,
                            BuildingName = p.BuildingName,
                            FloorName = p.FloorName,
                            CreatedDate = p.CreatedDate,
                            FmsProjectId = p.ProjectId,
                            SourceType = p.SourceType ?? 1
                        };

            if (sourceType == 1)
                query = query.Where(x => x.SourceType == 1 || ids.Contains(x.Id));
            else
                query = query.Where(x => ids.Contains(x.Id));

            condition.OrderBy = condition.OrderBy + " " + condition.AscDesc;
            var pageList = await query.ToPageList(condition);
            var result = condition.CopyTo<ApiPageResult>();
            result.PageData = pageList;
            result.Total = query.Count();
            return result;
        }
    }
}
